package com.ssh;

import java.util.HashSet;
import java.util.Set;

public class HasCycle_141 {
	public boolean hasCycle(ListNode1 head) {
		if (head == null) {
			return false;
		}
		Set<ListNode1> s = new HashSet<>();
		s.add(head);
		while (head.next != null) {
			if(s.contains(head.next)) {
				return true;
			}
			s.add(head.next);
			head = head.next;
		}
        return false;
    }
}
class ListNode1 {
	int val;
	ListNode1 next;
	ListNode1(int x) {
		val = x;
		next = null;
	}
}
